package org.redmagic.agent.spring;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
import org.springframework.util.StringValueResolver;

public class RedMagicStringValueResolver implements StringValueResolver {

	private final RedMagicPlaceholderResolver resolver;
	
	private String nullValue;
	
	private final Properties resolvedProperties;

	public RedMagicStringValueResolver() {

		this.resolvedProperties = new Properties();
		this.resolver = new RedMagicPlaceholderResolver(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, 
										                PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX);
	}

	public String resolveStringValue(String strVal) throws BeansException {

		String value = this.resolver.replacePlaceholders(strVal, resolvedProperties);
		return (value.equals(nullValue) ? null : value);
	}
	
	public Set<String> resolveStringValueAsync(String strVal) {
		
		Set<String> placeholders = new HashSet<String>();
		this.resolver.resolvePlaceholders(strVal, placeholders);
		
		return placeholders;
	}
	
	public void setResolvedProperties(Properties resolvedProperties) {
		
		this.resolvedProperties.putAll(resolvedProperties);
	}
}
